package com.clicacao.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class ClicacaoFunctions {
	
	private JSONParser jsonParser;
	
	//Endereco do servidor
	private static String url = "http://clicacao.com/clicacao/cliente/server_api/";
	
	//Poderia colocar as tags como variaveis estaticas aqui
	
	//Construtor da classe ClicacaoFunctions
	public ClicacaoFunctions(){
		jsonParser = new JSONParser();
	}
	
	/*
	 * Funcao para a solicitacao de login
	 * @param email
	 * @param password
	 */
	public JSONObject loginUser(String email, String password){
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo POST
		params.add(new BasicNameValuePair("tag", "login"));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
	
		return json;
	}
	
	/*
	 * Funcao para cadastrar usuario no sistema
	 * @param name
	 * @param email
	 * @param password
	 */
	
	public JSONObject registerUser(String name, String email, String password){
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo POST
		params.add(new BasicNameValuePair("tag", "register"));
		params.add(new BasicNameValuePair("username", name));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		//Seto inicialmente o id_avatar = 1 e o id_container = 1
		params.add(new BasicNameValuePair("id_avatar", "1"));
		//params.add(new BasicNameValuePair("id_container", "1"));
		
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
		
		return json;
		
	}
	
	/*
	 * Funcao para fazer o logout do usuario
	 */
	public boolean logoutUser(Context context){
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}
	
	/*
	 * Funcao para pegar as aventuras de um usuario 
	 * @param uid = user id
	 * 
	 */
	
	public JSONObject getUserAdventures(String uid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo POST
		params.add(new BasicNameValuePair("tag", "get_user_adventures"));
		params.add(new BasicNameValuePair("uid", uid));
		
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
		
		return json;
	}
	
	/*
	 * Funcao para pegar os objetos de uma determinada aventura
	 * @param aid = adventure id
	 */
	
	public JSONObject getAdventureObjectsUPIsActiveUsers(String aid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo post
		params.add(new BasicNameValuePair("tag", "get_adventure_objects"));
		params.add(new BasicNameValuePair("aid", aid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
		
		return json;
		
	}
	
	/*
	 * Funcao para atualizar a posicao de um usuario logado em uma aventura
	 * @param uid = user_id
	 */
	
	public JSONObject updateUserAdventurePosition(String uid, String latitude, String longitude){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo post
		params.add(new BasicNameValuePair("tag", "update_user_position"));
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("latitude", latitude));
		params.add(new BasicNameValuePair("longitude", longitude));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
				
		return json;	
	}
	
	/*
	 * Funcao para atualizar a situacao do usuario quanto a aventura no banco de dados
	 * @param uid = user_id
	 * @param aid = adventure_id
	 * @param situation
	 */
	
	public JSONObject updateAdventureUserSituation(String uid, String aid, String situation){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo post
		params.add(new BasicNameValuePair("tag", "update_adventure_user_situation"));
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("aid", aid));
		params.add(new BasicNameValuePair("situation", situation));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
				
		return json;
	}
	
	/*
	 * Funcao para inserir uma UPI criada por um usuario no banco de dados
	 * 
	 */
	public JSONObject insertUserUpi(String uid, String umid, String texto, String endereco, String latitude, String longitude, String tipo_upi){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//Passando os parametros para o metodo post
		params.add(new BasicNameValuePair("tag", "insert_user_upi"));
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("umid", umid));
		params.add(new BasicNameValuePair("texto", texto));
		params.add(new BasicNameValuePair("endereco", endereco));
		params.add(new BasicNameValuePair("latitude", latitude));
		params.add(new BasicNameValuePair("longitude", longitude));
		params.add(new BasicNameValuePair("tipo_upi", tipo_upi));

		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
				
		return json;
	}
	
	/*
	 * Funcao para pegar as UPIs que estao na mochila de determinado usuario
	 */
	
	public JSONObject getUserBagUpis(String umid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "get_user_bag_upis"));
		params.add(new BasicNameValuePair("umid", umid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para pegar os objetos que estao na mochila de um determinado usuario
	 */
	
	public JSONObject getUserBagObjects(String umid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "get_user_bag_objects"));
		params.add(new BasicNameValuePair("umid", umid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para largar uma UPI no mapa
	 * 
	 */
	
	public JSONObject dropUpiToAdventure(String upiid, String aid, String latitude, String longitude){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "drop_user_upi_to_adventure"));
		params.add(new BasicNameValuePair("upiid", upiid));
		params.add(new BasicNameValuePair("aid", aid));
		params.add(new BasicNameValuePair("latitude", latitude));
		params.add(new BasicNameValuePair("longitude", longitude));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para largar um objeto na aventura
	 */
	
	public JSONObject dropObjectToAdventure(String oid, String aid, String latitude, String longitude){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "drop_user_object_to_adventure"));
		params.add(new BasicNameValuePair("oid", oid));
		params.add(new BasicNameValuePair("aid", aid));
		params.add(new BasicNameValuePair("latitude", latitude));
		params.add(new BasicNameValuePair("longitude", longitude));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para pegar uma UPI do mapa
	 */
	
	public JSONObject getUpiFromAdventureToBag(String upiid, String umid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "get_upi_from_adventure_to_bag"));
		params.add(new BasicNameValuePair("upiid", upiid));
		params.add(new BasicNameValuePair("umid", umid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para pegar um objeto do mapa
	 */
	
	public JSONObject getObjectFromAdventureToBag(String oid, String uid, String umid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "get_object_from_adventure_to_bag"));
		params.add(new BasicNameValuePair("oid", oid));
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("umid", umid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para pegar as missoes de um usuario em uma aventura
	 */
	
	public JSONObject getUserAdventureMissions(String uid, String aid){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "get_user_adventure_missions"));
		params.add(new BasicNameValuePair("uid", uid));
		params.add(new BasicNameValuePair("aid", aid));
		params.add(new BasicNameValuePair("uid", uid));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
	/*
	 * Funcao para atualizar o status de uma missao de usuario
	 */
	
	public JSONObject updateUserAdventureMissionStatus(String muid, String status){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", "update_adventure_mission_status"));
		params.add(new BasicNameValuePair("muid", muid));
		params.add(new BasicNameValuePair("status", status));
		
		//Instanciando um metodo JSON
		JSONObject json = new JSONObject();
		json = jsonParser.getJSONFromUrl(url, params);
						
		return json;
	}
	
}
